package me.zhengjie.modules.system.rest;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.MessageComplete;
import me.zhengjie.modules.system.domain.TeskAndstudent;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.MessageCompleteService;
import me.zhengjie.modules.system.service.TeskAndstudentService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequiredArgsConstructor
@Api(tags = "消息任务完成")
@RequestMapping("/api/messageComplete")
public class MessageCompleteController {

    private final MessageCompleteService messageCompleteService;

    private final UserService userService;

    private final TeskAndstudentService teskAndstudentService;
    @PostMapping
    @ApiOperation("消息任务完成")
    public ResponseEntity<MessageComplete> createMessageComplete(@RequestBody MessageComplete messageComplete) {
        QueryWrapper   queryWrapper = new QueryWrapper();
        queryWrapper.eq("teskId",messageComplete.getTeskId());
        MessageComplete messageComplete1 = messageCompleteService.getOne(queryWrapper);
        User user = userService.findByName(SecurityUtils.getCurrentUsername());
        TeskAndstudent teskAndstudent = new TeskAndstudent();

        if(messageComplete1!=null){
            messageComplete.setId(messageComplete1.getId());
            messageCompleteService.updateById(messageComplete);

            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("teskId", messageComplete.getTeskId());
            queryWrapper1.eq("userId", Math.toIntExact(user.getId()));
            teskAndstudentService.getOne(queryWrapper1);


            QueryWrapper queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("userId",user.getId());
            queryWrapper2.eq("teskId",messageComplete.getTeskId());
            TeskAndstudent  teskAndstudent1 = teskAndstudentService.getOne(queryWrapper2);
            teskAndstudent1.setState(messageComplete.getState());
            teskAndstudentService.updateTeskAndstudent(teskAndstudent1);

        } else {

            messageCompleteService.save(messageComplete);

            QueryWrapper queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("userId",user.getId());
            queryWrapper2.eq("teskId",messageComplete.getTeskId());
            TeskAndstudent  teskAndstudent1 = teskAndstudentService.getOne(queryWrapper2);
            teskAndstudent1.setState(messageComplete.getState());
            teskAndstudentService.updateTeskAndstudent(teskAndstudent1);

        }
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
}
